package com.duff.utils;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.jeff.utils.thread.ParallelScheduler;
import com.jeff.utils.thread.SerialScheduler;
import com.jeff.utils.thread.Task;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author: duff
 * @date: 2019/3/31
 * @since: 1.0.0
 */
public class ThreadTestActivity extends AppCompatActivity {

    private final static String TAG = "_thread_test";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_test);

        findViewById(R.id.bt1)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        SerialScheduler.execute(new Runnable() {
                            @Override
                            public void run() {
                                Log.d(TAG, "第一个任务后台");
                            }
                        });

                        SerialScheduler.execute(new Task<String, String>(null) {
                            @Override
                            public String doInBackground(String s) {
                                try {
                                    Thread.sleep(3000);
                                    Log.d(TAG, "第二个任务后台,我休眠了 3 秒");
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                return "Hello, World !!";
                            }

                            @Override
                            public void onPostExecute(String s) {
                                Log.d(TAG, "第二个任务前台：" + s);
                            }
                        });

                        final Future<String> future = SerialScheduler.submit(
                                new Task<String, String>(null) {
                                    @Override
                                    public String doInBackground(String s) {
                                        Log.d(TAG, "第三个任务后台");
                                        return "我来自第三个任务";
                                    }
                                });

                        SerialScheduler.execute(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Log.d(TAG, "第四个任务后台，收到消息：" + future.get());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                    }
                });


        findViewById(R.id.bt2)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        ParallelScheduler.execute(new Runnable() {
                            @Override
                            public void run() {
                                for (int i = 0; i < 5; i++) {
                                    Log.d(TAG, "任务一:"+i);
                                    try {
                                        Thread.sleep(3000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        });

                        ParallelScheduler.execute(new Task<String, String>(null) {
                            @Override
                            public String doInBackground(String s) {
                                for (int i = 0; i < 5; i++) {
                                    Log.d(TAG, "任务二:"+i);
                                    try {
                                        Thread.sleep(2000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                                return null;
                            }

                            @Override
                            public void onPostExecute(String s) {
                                Log.d(TAG, "任务二前台");
                            }
                        });

                        ParallelScheduler.execute(
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        for (int i = 0; i < 5; i++) {
                                            Log.d(TAG, "任务三:"+i);
                                            try {
                                                Thread.sleep(5000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                },
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        Log.d(TAG, "任务三前台");
                                    }
                                }
                        );

                    }
                });


    }


}
